<template>
	<view class="addDevice">
		<u-navbar title="添加设备" back-icon-color="#fff"
			:background="{backgroundImage: 'linear-gradient(45deg, #68dae7, #21a4b3)'}" title-color="#FFFFFF">
		</u-navbar>
		<view class="addDevice-info">
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text required">序列号</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.serialNumber" placeholder=" "/>
				<!-- <u-icon name="scan" color="#00aa00" size="32" @tap.stop="scanCode"></u-icon> -->
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text required">资产编号</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.assetNumber" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text required">仪器名称</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.deviceName" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text required">仪器型号</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.deviceType" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">客户单位</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.company" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">客户部门</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.dept" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">负责人</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.owner" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">负责人电话</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.phone" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">巡检周期(天)</text>
				<u-input class="addDevice-info-item-put" :border="true" type="number" v-model="device.xjCycle" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">核查周期(天)</text>
				<u-input class="addDevice-info-item-put" :border="true" type="number" v-model="device.hcCycle" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">保养周期(天)</text>
				<u-input class="addDevice-info-item-put" :border="true" type="number" v-model="device.byCycle" placeholder=" "/>
			</view>
			<view class="addDevice-info-item">
				<text class="addDevice-info-item-text">清单配置</text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="device.configurationSheet" placeholder=" "/>
				<!-- <u-icon name="plus" color="#00aa00" size="32" @tap="addConfig"></u-icon> -->
			</view>
			<view class="addDevice-info-item" v-for="(item,index) in configList" :key="index">
				<text class="addDevice-info-item-text"></text>
				<u-input class="addDevice-info-item-put" :border="true" type="text" v-model="item.val" placeholder=" "/>
				<u-icon name="minus" color="#00aa00" size="32" @tap.stop="deleteConfig(index)"></u-icon>
			</view>
			<view class="addDevice-info-img" >
				<view>仪器照片</view>
				<u-upload ref="uUpload" :max-count="1" :action="action"  @on-success="uploadSuccess" @on-error="uploadError" :header="uploadHeader" :auto-upload="false" ></u-upload>
			</view>
		</view>
		<view class="addDevice-submit">
			<view class="addDevice-submit-btn" @tap="saveDevice">保存</view>
		</view>
		<xt-loading :showMask="showMask"></xt-loading>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				snError:"",
				device:{},
				config:"",
				configList:[],
				action:uni.getStorageSync("baseUrl")+"/wx/upload",
				uploadHeader:{
					"Content-Type": "multipart/form-data",
					"token":uni.getStorageSync("token"),
				},
				showMask:false
			}
		},
		methods:{
			scanCode(){
				uni.scanCode({
					success(res){
						
					}
				})
			},
			saveDevice(){
				if(!this.device.serialNumber){
					this.$refs.uToast.show({
						title:"序列号不能为空",
						duration:2000,
						type:"error"
					})
					return
				}
				if(!this.device.assetNumber){
					this.$refs.uToast.show({
						title:"资产编号不能为空",
						duration:2000,
						type:"error"
					})
					return
				}
				if(!this.device.deviceName){
					this.$refs.uToast.show({
						title:"仪器名称不能为空",
						duration:2000,
						type:"error"
					})
					return
				}
				if(!this.device.deviceType){
					this.$refs.uToast.show({
						title:"仪器类型不能为空",
						duration:2000,
						type:"error"
					})
					return
				}
				if(!this.device.xjCycle){
					this.device.isXj = 1
					this.device.xjCycle = ""
				}else{
					this.device.isXj = 2
				}
				if(!this.device.byCycle){
					this.device.isBy = 1
					this.device.byCycle = ""
				}else{
					this.device.isBy = 2
				}
				if(!this.device.hcCycle){
					this.device.isHc = 1
					this.device.hcCycle = ""
				}else{
					this.device.isHc = 2
				}
				if(this.$refs.uUpload.lists.length>0){
					this.showMask = true
					this.$refs.uUpload.upload()
				}else{
					this.showMask = true
					this.saveInfo()
				}
			},
			saveInfo(){
				this.$xt.api.deviceAdd(this.device).then(res=>{
					this.showMask = false
					this.$refs.uToast.show({
						title:"操作成功",
						duration:2000,
						type:"success"
					})
					this.device = {}
					this.$refs.uUpload.clear()
				}).catch(res=>{
					this.showMask = false
					this.$refs.uUpload.clear()
					this.$refs.uToast.show({
						title:res.msg,
						duration:2000,
						type:"error"
					})
				})
			},
			uploadSuccess(data, index, lists, name){
				this.device.photo= data.url
				this.saveInfo()
			},
			uploadError(res){
				this.showMask = false
				this.$refs.uToast.show({
					title:res.msg,
					duration:2000,
					type:"error"
				})
			},
			addConfig(){
				this.configList.push({val:""})
			},
			deleteConfig(idx){
				this.configList.splice(idx,1)
			}
		}
	}
</script>

<style scoped lang="scss">
	.addDevice{
		&-info{
			background-color: #fff;
			&-item{
				display: flex;
				padding: 10rpx 24rpx;
				align-items: center;
				&-text{
					margin-right: 24rpx;
					width: 80px;
				}
				&-put{
					flex:1;
					margin-right: 20rpx;
				}
			}
			&-img{
				padding: 10rpx 24rpx 24rpx 24rpx;
			}
		}
		
		&-submit{
			position: fixed;
			bottom: 10rpx;
			width: 100%;
			background-color: #fff;
			padding: 20rpx 40rpx;
			&-btn{
				text-align: center;
				background-color: #00aa00;
				color:#fff;
				padding: 10rpx 40rpx;
				border-radius: 15rpx;
			}
		}
	}
	.required{
		padding-left: -5px;
	}
	.required::before{
		content: "*";
		color:red
	}
</style>